import { resetCamera, flyToCenter } from "../camera/index.ts";
import { defaultCoordinates } from "../../mapbox_config/mapConfigManager/index";
import { eventManager } from "./eventManager.ts";
import { showSuccessMessage, showWarningMessage } from "../message/index.ts";
// import { initializationMapMgr } from "./initializationMap";
import { contextMenuConfig } from "./config.ts";
import ContextMenu from "./contextmenu/index.vue";
import { createApp } from "vue";
import { DistanceMeasureTool, AreaMeasureTool, PointMarkerTool } from "../draw/mapboxDrawWrapper.ts"; // 绘制工具
import MapboxDraw from "@mapbox/mapbox-gl-draw";

let contextMenuInstance: any = null;
let currentCoordinates: any = [];
let drawWrapperTool: DistanceMeasureTool; // 测距工具
let measureAreaTool: AreaMeasureTool; // 测量面积工具
let pointMarkerTool: PointMarkerTool; // 点标记工具

// 初始化右键菜单组件
function initContextMenuComponent() {
  const contextMenuDiv = document.createElement("div");
  document.body.appendChild(contextMenuDiv);

  const app = createApp(ContextMenu, {
    menuItems: contextMenuConfig
  });

  contextMenuInstance = app.mount(contextMenuDiv);
}
// 初始化事件
export function initRightMenu(map: any) {
  const draw = new MapboxDraw({
    displayControlsDefault: false,
    controls: {
      polygon: true,
      point: true,
      trash: true,
      line_string: true
    },
    userProperties: true
    // defaultMode: 'draw_polygon'
  });
  map.addControl(draw);

  drawWrapperTool = new DistanceMeasureTool(map, draw); // 初始化测距工具
  measureAreaTool = new AreaMeasureTool(map, draw); // 测量面积
  pointMarkerTool = new PointMarkerTool(map, draw); // 点标记工具
  initContextMenuComponent();
  eventManager.addEventListener(map, "contextmenu", contextmenuMap);
}

// 右击事件
function contextmenuMap(e: any) {
  e.preventDefault(); // 阻止默认右击菜单
  if (e.lngLat) {
    currentCoordinates = e.lngLat;
    contextMenuInstance.showAt(e.originalEvent.clientX, e.originalEvent.clientY);
  }
}
// 清除右击事件
export function clearRightMenu(map: any) {
  eventManager.removeEventListener(map, "contextmenu", contextmenuMap);
}

// 重置地图
export function resetMap(map: any) {
  resetCamera(map);
  flyToCenter(map, defaultCoordinates, { zoom: 4.5 });
}

// 查看此处坐标
export function showCoordinates() {
  showSuccessMessage("右击坐标:" + currentCoordinates.lng + "," + currentCoordinates.lat);
}

//视角切换 2D/3D/2.5D
export function viewChange(map: any, type: string) {
  // initializationMapMgr.changeMapMode(map, type);
  showWarningMessage("当前不支持切换视角");
  console.log(map, type);
}

// 移动到此处
export function moveToHere(map: any) {
  flyToCenter(map, currentCoordinates, { zoom: 6.5 });
}

// 第一人称视角
export function firstPerspective(map: any) {
  flyToCenter(map, currentCoordinates, {
    zoom: 17,
    pitch: 80,
    bearing: map.getBearing(),
    duration: 2000
  });
}

// 打开键盘漫游
export function openKeyboardRoaming(map: any) {
  if (!map || !map.keyboard) {
    console.error("Map 对象未正确初始化");
    return;
  }
  map.keyboard.enable();
  showSuccessMessage("键盘漫游已开启，按键↑，↓，←，→进行移动");
}

// 关闭键盘漫游
export function closeKeyboardRoaming(map: any) {
  map.keyboard.disable();
  showSuccessMessage("键盘漫游已关闭");
}

// 空间测距
export function spaceDistance() {
  drawWrapperTool.start();
  // 一下是测试移除功能
  // setTimeout(() => {
  //     drawWrapperTool.stop()
  // }, 10000);
}

// 测量面积
export function spaceArea() {
  measureAreaTool.start();
  // 一下是测试移除功能
  setTimeout(() => {
    measureAreaTool.stop();
  }, 10000);
}

// 点标记
export function pointMarker() {
  pointMarkerTool.start();
  // 一下是测试移除功能
  // setTimeout(() => {
  //     pointMarkerTool.stop()
  // }, 10000);
}
